"""测试机器学习模型完整性"""

# from sentence_transformers import SentenceTransformer
# import os
#
# model_path = "./models/sentence-transformer"
#
# print("🔍 正在验证模型完整性...")
#
# try:
#     model = SentenceTransformer(model_path)
#     print("✅ 模型加载成功！结构完整。")
#
#     # 测试编码一句话
#     test_sentence = "这是一个测试句子。"
#     embedding = model.encode(test_sentence)
#     print(f"✅ 成功生成向量，维度: {embedding.shape}")
#     print(f"✅ 示例向量前5维: {embedding[:5]}")
#
# except Exception as e:
#     print(f"❌ 模型加载失败: {e}")
#     print("请检查是否缺少以下文件:")
#     print("  - config.json")
#     print("  - modules.json")
#     print("  - tf_model.h5 或 pytorch_model.bin")
#     print("  - tokenizer 相关文件")


# from sentence_transformers import SentenceTransformer
#
# # 加载本地模型
# model = SentenceTransformer('./models/sentence-transformer')
#
# sentences = [
#     "我喜欢学习人工智能。",
#     "Hugging Face 是一个很棒的平台。",
#     "今天不想上班。"
# ]
#
# embeddings = model.encode(sentences)
# print(f"编码 {len(sentences)} 个句子，形状: {embeddings.shape}")  # (3, 384)




# from sentence_transformers import util
# from sentence_transformers import SentenceTransformer
#
# # 加载本地模型
# model = SentenceTransformer('./models/sentence-transformer')
# sentences = ["猫在沙发上睡觉", "狗在地毯上打滚", "猫咪正在休息"]
#
# embeddings = model.encode(sentences)
#
# # 计算余弦相似度
# cos_sim = util.cos_sim(embeddings, embeddings)
# print("句子相似度矩阵:")
# print(cos_sim)
#
# # 找出最相似的句子对
# for i in range(len(sentences)):
#     for j in range(i+1, len(sentences)):
#         sim = cos_sim[i][j]
#         print(f"'{sentences[i]}' 与 '{sentences[j]}' 相似度: {sim:.4f}")





# test_model.py
from sentence_transformers import SentenceTransformer, util
import numpy as np

print("🚀 正在加载本地模型...")
model = SentenceTransformer('./models/sentence-transformer')
print("✅ 模型加载成功！")

# 测试编码
test_sentences = [
    "自然语言处理很有趣",
    "我喜欢用 Hugging Face",
    "今天不想写代码"
]

print("\n📝 正在编码句子...")
embeddings = model.encode(test_sentences)

print(f"✅ 编码完成，形状: {embeddings.shape}")

# 计算相似度
print("\n🧮 计算句子相似度...")
similarity_matrix = util.cos_sim(embeddings, embeddings)
print("相似度矩阵:")
print(np.round(similarity_matrix.numpy(), 3))

# 找最相似的一对
max_sim = 0
best_pair = ("", "")
for i in range(len(test_sentences)):
    for j in range(i+1, len(test_sentences)):
        sim = similarity_matrix[i][j].item()
        if sim > max_sim:
            max_sim = sim
            best_pair = (test_sentences[i], test_sentences[j])

print(f"\n🏆 最相似的句子对: '{best_pair[0]}' 和 '{best_pair[1]}' (相似度: {max_sim:.4f})")





















